package com.traffic.collect.task;


import com.traffic.collect.entity.TraCollection;
import com.traffic.collect.service.ICollectService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;
import oshi.software.os.OperatingSystem;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

@Component
@Slf4j
public class ScheduledTasks {

    @Autowired
    private ICollectService collectService;

    private final SystemInfo systemInfo = new SystemInfo();
    private final HardwareAbstractionLayer hardware = systemInfo.getHardware();
    private final CentralProcessor processor = hardware.getProcessor();
    private final GlobalMemory memory = hardware.getMemory();
    private long[] prevTicks = processor.getSystemCpuLoadTicks();
    private final OperatingSystem os = systemInfo.getOperatingSystem();



    @Value("${scheduled.fixedRate}")
    private long fixedRate;

    @Value("${scheduled.cron}")
    private String cronExpression;

    // 每隔固定时间间隔执行一次
    @Scheduled(fixedRateString = "${scheduled.fixedRate}")
    public void performTask() throws UnknownHostException {
        InetAddress inetAddress = InetAddress.getLocalHost();
        long[] ticks = processor.getSystemCpuLoadTicks();
        double cpuLoad = processor.getSystemCpuLoadBetweenTicks(prevTicks) * 100;
        prevTicks = ticks;



        // 获取内存使用率
        long totalMemory = memory.getTotal();
        long availableMemory = memory.getAvailable();
        double memoryUsage = ((double) (totalMemory - availableMemory) / totalMemory) * 100;

        // 获取硬盘使用率
        FileSystem fileSystem = os.getFileSystem();
        List<OSFileStore>  fileStores = fileSystem.getFileStores();
        long totalDisk = 0;
        long usableDisk = 0;
        for (OSFileStore fs : fileStores) {
            totalDisk += fs.getTotalSpace();
            usableDisk += fs.getUsableSpace();
        }
        double diskUsage = ((double) (totalDisk - usableDisk) / totalDisk) * 100;
//        log.info("当前CPU使用率: {}%", String.format("%.2f", cpuLoad));
//        log.info("当前内存使用率: {}%", String.format("%.2f", memoryUsage));
//        log.info("当前硬盘使用率: {}%", String.format("%.2f", diskUsage));
//        log.info("Ip地址为：{}",inetAddress.getHostAddress());
        TraCollection collection=TraCollection.builder().ip(inetAddress.getHostAddress())
                .memoryValue(Double.valueOf(String.format("%.2f", memoryUsage))).cpuValue(Double.valueOf(String.format("%.2f", cpuLoad)))
                .hardValue(Double.valueOf(String.format("%.2f", diskUsage))).build();
        collectService.doCollect(collection);
    }

    // 根据cron表达式执行
    @Scheduled(cron = "${scheduled.cron}")
    public void performDailyTask() {
        log.info("每日任务执行: " + System.currentTimeMillis());
    }
}
